<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 测试用例执行计划（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某个产品当前迭代周期内有 N 个特性&#xff08;F1,F2,......FN&#xff09;需要进行覆盖测试&#xff0c;每个特性都被评估了对应的优先级&#xff0c;特性使用其 ID 作为下标进行标识。</p> 
<p>设计了 M 个测试用例&#xff08;T1,T2,......,TM&#xff09;&#xff0c;每个测试用例对应一个覆盖特性的集合&#xff0c;测试用例使用其 ID 作为下标进行标识&#xff0c;测试用例的优先级定义为其覆盖的特性的优先级之和。</p> 
<p>在开展测试之前&#xff0c;需要制定测试用例的执行顺序&#xff0c;规则为&#xff1a;优先级大的用例先执行&#xff0c;如果存在优先级相同的用例&#xff0c;用例 ID 小的先执行。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入为 N 和 M&#xff0c;</p> 
<ul><li>N 表示特性的数量&#xff0c;0 &lt; N ≤ 100</li><li>M 表示测试用例的数量&#xff0c;0 &lt; M ≤ 100</li></ul> 
<p>之后 N 行表示特性 ID&#61;1 到特性 ID&#61;N 的优先级&#xff0c;</p> 
<p>再接下来 M 行表示测试用例 ID&#61;1 到测试用例 ID&#61;M 关联的特性的 ID 的列表。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>按照执行顺序&#xff08;优先级从大到小&#xff09;输出测试用例的 ID&#xff0c;每行一个ID。</p> 
<p>测试用例覆盖的 ID 不重复。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5 4<br /> 1<br /> 1<br /> 2<br /> 3<br /> 5<br /> 1 2 3<br /> 1 4<br /> 3 4 5<br /> 2 3 4</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3<br /> 4<br /> 1<br /> 2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>测试用例的优先级计算如下&#xff1a;</p> <p>T1 &#61; Pf1 &#43; Pf2 &#43; Pf3 &#61; 1 &#43; 1 &#43; 2 &#61; 4<br /> T2 &#61; Pf1 &#43; Pf4 &#61; 1 &#43; 3 &#61; 4<br /> T3 &#61; Pf3 &#43; Pf4 &#43; Pf5 &#61; 2 &#43; 3 &#43; 5 &#61; 10<br /> T4 &#61; Pf2 &#43; Pf3 &#43; Pf4 &#61; 1 &#43; 2 &#43; 3 &#61; 6</p> <p>按照优先级从小到大&#xff0c;以及相同优先级&#xff0c;ID小的先执行的规则&#xff0c;执行顺序为T3,T4,T1,T2</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3 3<br /> 3<br /> 1<br /> 5<br /> 1 2 3<br /> 1 2 3<br /> 1 2 3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1<br /> 2<br /> 3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>测试用例的优先级计算如下&#xff1a;</p> <p>T1 &#61; Pf1 &#43; Pf2 &#43; Pf3 &#61; 3 &#43; 1 &#43; 5 &#61; 9<br /> T2 &#61; Pf1 &#43; Pf2 &#43; Pf3 &#61; 3 &#43; 1 &#43; 5 &#61; 9<br /> T3 &#61; Pf1 &#43; Pf2 &#43; Pf3 &#61; 3 &#43; 1 &#43; 5 &#61; 9</p> <p>每个优先级一样&#xff0c;按照 ID 从小到大执行&#xff0c;执行顺序为T1,T2,T3</p> </td></tr></tbody></table> 
<p> </p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单的自定义排序问题。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" style="background-color:transparent;">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const [n, m] &#61; (await readline()).split(&#34; &#34;).map(Number);

  const features &#61; new Array(n &#43; 1);
  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    features[i] &#61; parseInt(await readline());
  }

  const cases &#61; [];
  for (let i &#61; 1; i &lt;&#61; m; i&#43;&#43;) {
    const priority &#61; (await readline())
      .split(&#34; &#34;)
      .map((id) &#61;&gt; features[id - 0]) // id-0是为了将字符串id转为数值id
      .reduce((a, b) &#61;&gt; a &#43; b);

    cases.push([priority, i]);
  }

  cases
    .sort((a, b) &#61;&gt; (a[0] !&#61; b[0] ? b[0] - a[0] : a[1] - b[1]))
    .forEach(([_, id]) &#61;&gt; console.log(id)); // forEach入参使用了数组解构语法
})();
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
  static class TestCase {
    int id;
    int priority;

    public TestCase(int id, int priority) {
      this.id &#61; id;
      this.priority &#61; priority;
    }
  }

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int[] tmp &#61; Arrays.stream(sc.nextLine().split(&#34; &#34;)).mapToInt(Integer::parseInt).toArray();
    int n &#61; tmp[0];
    int m &#61; tmp[1];

    int[] features &#61; new int[n &#43; 1];
    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
      features[i] &#61; Integer.parseInt(sc.nextLine());
    }

    ArrayList&lt;TestCase&gt; cases &#61; new ArrayList&lt;&gt;();

    for (int i &#61; 1; i &lt;&#61; m; i&#43;&#43;) {
      int priority &#61;
          Arrays.stream(sc.nextLine().split(&#34; &#34;))
              .map(Integer::parseInt)
              .map(id -&gt; features[id])
              .reduce(Integer::sum)
              .orElse(0);

      cases.add(new TestCase(i, priority));
    }

    cases.stream()
        .sorted((a, b) -&gt; a.priority !&#61; b.priority ? b.priority - a.priority : a.id - b.id)
        .map(testcase -&gt; testcase.id)
        .forEach(System.out::println);
  }
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n, m &#61; map(int, input().split())

features &#61; [0] * (n &#43; 1)
for i in range(1, n&#43;1):
    features[i] &#61; int(input())

cases &#61; []
for i in range(1, m&#43;1):
    priority &#61; sum(map(lambda x: features[int(x)], input().split()))
    cases.append([priority, i])

cases.sort(key&#61;lambda x: (-x[0], x[1]))

for _, idx in cases:
    print(idx)</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

typedef struct {
    int priority;
    int id;
} TestCase;

TestCase *new_TestCase(int id) {
    TestCase *testcase &#61; (TestCase *) malloc(sizeof(TestCase));
    testcase-&gt;priority &#61; 0;
    testcase-&gt;id &#61; id;
    return testcase;
}

int cmp(const void *a, const void *b) {
    TestCase *A &#61; *((TestCase **) a);
    TestCase *B &#61; *((TestCase **) b);
    return A-&gt;priority !&#61; B-&gt;priority ? B-&gt;priority - A-&gt;priority : A-&gt;id - B-&gt;id;
}

int main() {
    int n, m;
    scanf(&#34;%d %d&#34;, &amp;n, &amp;m);

    int features[n &#43; 1];
    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;features[i]);
    }

    TestCase *cases[m];
    for (int i &#61; 0; i &lt; m; i&#43;&#43;) {
        cases[i] &#61; new_TestCase(i &#43; 1);

        int feature_id;
        while (scanf(&#34;%d&#34;, &amp;feature_id)) {
            cases[i]-&gt;priority &#43;&#61; features[feature_id];
            if (getchar() !&#61; &#39; &#39;) break;
        }
    }

    qsort(cases, m, sizeof(TestCase *), cmp);

    for (int i &#61; 0; i &lt; m; i&#43;&#43;) {
        printf(&#34;%d\n&#34;, cases[i]-&gt;id);
    }

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>